using Digitalmes.Application.Scada.Events;
using Digitalmes.Application.Services;

namespace Digitalmes.Application.Scada.EventHandlers;

/// <summary>
/// 推送数据。
/// </summary>
internal sealed class RemoteSynchronousHandler(
    ISysConfigQueryService sysConfigQueryService,
    IRemoteSynchronousService dataSyncService,
    ILogger<RemoteSynchronousHandler> logger) : INotificationHandler<AfterOutboundEvent>
{
    public async Task Handle(AfterOutboundEvent notification, CancellationToken cancellationToken)
    {
        try
        {
            if (!await sysConfigQueryService.IsYes(ConfigKeys.AllowSynchronousToRemote))
            {
                return;
            }

            // 推送数据参数
            await dataSyncService.PushAsync(notification.Output, cancellationToken);
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "[存档后处理事件异常] {SN}, 产线：{LineCode}，工位：{OpCode}",
                 notification.Output.Sn,
                 notification.Output.LineCode,
                 notification.Output.WorkstationCode);
        }
    }
}
